package com.arm.tools;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.arm.tools.json.JsonHandler;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ReturnCode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Baggio on 2019-9-29.
 */
public class AliSMSTools {
    private final static String URL = "http://gw.api.taobao.com/router/rest";
    private final static String ACCESS_KEY_ID = "LTAII31XbXRP0G6f";
    private final static String ACCESS_KEY_SECRET = "j0byMopwJzBhENB79UDBv1giNMS0l6";
    /**
     * 注册验证码
     */
    public final static String SMS_TEMPLATE_REGEDIT = "SMS_191835075";



    public  static void sendSMS(Map<String,String> templateParam, String phoneNumbers, String SMSTemplateCode)  {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "秋雨晨");
        request.putQueryParameter("TemplateCode", SMSTemplateCode);
        request.putQueryParameter("TemplateParam", JsonHandler.MapToJson(templateParam));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            Map<String, Object> map= JsonHandler.json2Map(response.getData());
            String code=(String)map.get("Code");
            if(!code.equals("OK")){
                throw new MyRunTimeExcption(ReturnCode.SMS_SEND_FAIL);
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        Map<String,String> map=new HashMap<>();
        map.put("code","123456");
        AliSMSTools.sendSMS(map,"13618267496",AliSMSTools.SMS_TEMPLATE_REGEDIT);
    }

}
